package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.*;
import org.dromara.system.service.*;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * Fault Management (English Admin Side) Controller
 *
 * @author system
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/fault-en")
public class SysFaultManageEnController extends BaseController {

    private final ISysFaultInfoEnService faultInfoService;
    private final ISysSolutionDocEnService solutionDocService;

    // ==================== Fault Info Management ====================

    /**
     * Query fault info list
     */
    @GetMapping("/list")
    public TableDataInfo<SysFaultInfoEnVo> list(
            SysFaultInfoEnQueryBo queryBo, PageQuery pageQuery) {
        return faultInfoService.queryPageList(queryBo, pageQuery);
    }

    /**
     * Export fault info list
     */
    @SaCheckPermission("system:faultEn:export")
    @Log(title = "English Fault Info", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysFaultInfoEnQueryBo queryBo, HttpServletResponse response) {
        List<SysFaultInfoEnVo> list = faultInfoService.queryList(queryBo);
        ExcelUtil.exportExcel(list, "Fault Information (English)", 
                            SysFaultInfoEnVo.class, response);
    }

    /**
     * Get fault info detail
     */
    @GetMapping("/{faultId}")
    public R<SysFaultInfoEnVo> getInfo(@PathVariable Long faultId) {
        return R.ok(faultInfoService.queryById(faultId));
    }

    /**
     * Add fault info (Integrated: Fault + Solution)
     */
    @SaCheckPermission("system:faultEn:add")
    @Log(title = "English Fault Info", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysFaultInfoEnVo> add(
            @RequestPart("fault") @Validated(AddGroup.class) SysFaultManageEnBo faultManage,
            @RequestPart(value = "files", required = false) MultipartFile[] files) {
        SysFaultInfoEnVo vo = faultInfoService.createFaultWithSolution(faultManage, files);
        return R.ok(vo);
    }

    /**
     * Update fault info (Integrated: Fault + Solution)
     */
    @SaCheckPermission("system:faultEn:edit")
    @Log(title = "English Fault Info", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysFaultInfoEnVo> edit(
            @RequestPart("fault") @Validated(EditGroup.class) SysFaultManageEnBo faultManage,
            @RequestPart(value = "files", required = false) MultipartFile[] files) {
        SysFaultInfoEnVo vo = faultInfoService.updateFaultWithSolution(faultManage, files);
        return R.ok(vo);
    }

    /**
     * Delete fault info (Cascade delete solution)
     */
    @SaCheckPermission("system:faultEn:remove")
    @Log(title = "English Fault Info", businessType = BusinessType.DELETE)
    @DeleteMapping("/{faultIds}")
    public R<Void> remove(
            @NotEmpty(message = "ID cannot be empty") @PathVariable Long[] faultIds,
            @RequestParam(defaultValue = "auto") String cascadeStrategy) {
        return toAjax(faultInfoService.deleteFaultWithCascade(
            Arrays.asList(faultIds), cascadeStrategy));
    }

    // ==================== Cascade Query APIs ====================
    
    @GetMapping("/models")
    public R<List<String>> getExcavatorModels() {
        List<String> models = faultInfoService.getExcavatorModelList();
        return R.ok(models);
    }

    @GetMapping("/parts")
    public R<List<String>> getFaultParts(@RequestParam(required = false) String excavatorModel) {
        List<String> parts = faultInfoService.getFaultPartList(excavatorModel);
        return R.ok(parts);
    }

    @GetMapping("/symptoms")
    public R<List<String>> getFaultSymptoms(
            @RequestParam(required = false) String excavatorModel,
            @RequestParam(required = false) String faultPart) {
        List<String> symptoms = faultInfoService.getFaultSymptomList(excavatorModel, faultPart);
        return R.ok(symptoms);
    }

    // ==================== Solution Doc Management ====================

    @GetMapping("/doc/list")
    public TableDataInfo<SysSolutionDocEnVo> docList(
            SysSolutionDocEnBo queryBo, PageQuery pageQuery) {
        return solutionDocService.queryPageList(queryBo, pageQuery);
    }

    @GetMapping("/doc/{docId}")
    public R<SysSolutionDocEnVo> getDocInfo(@PathVariable Long docId) {
        return R.ok(solutionDocService.queryById(docId));
    }

    @GetMapping("/doc/preview/{docId}")
    public void previewDoc(
            @PathVariable Long docId, 
            @RequestParam(defaultValue = "inline") String disposition,
            HttpServletResponse response) throws Exception {
        solutionDocService.previewDocument(docId, disposition, response);
    }
}

